import { verifyAccessToken } from '~/utils/jwt-utils';
import { unAuthorizedResponse, useResponseSuccess, customResponse } from '~/utils/response';
import { queryDatabase } from '~/functions';

export default eventHandler(async (event) => {
  const userinfo = verifyAccessToken(event);
  if (!userinfo) {
    return unAuthorizedResponse(event);
  }

  try {
    let queryString;
    // 读取GET请求的参数
    const { sampleNo } = await getQuery(event);
    if (!sampleNo) throw new Error('样品编号不能为空！');

    queryString = `SELECT * FROM samplelists WHERE sampleNo = ?`;

    // 调用 queryDatabase 函数执行操作
    const results = <any[]>await queryDatabase(queryString, sampleNo);

    if (results.length <= 0) {
      throw new Error(`数据库中无编号为：${sampleNo}！的数据！`);
    } else {
      return useResponseSuccess(results);
    }
  } catch (error) {
    return customResponse(-2, error.message);
  }
});
